package vectors;

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.Iterator;

/**
 * Created with IntelliJ IDEA.
 * User: platonovv
 * Date: 24.10.13
 * Time: 18:45
 */
public class ArrayListVector extends Vector implements VectorOperations {
    private List<Double> vector = null;

    private ArrayListVector() {
    };

    public ArrayListVector(int length) {
        vector = new ArrayList(length);
    };

    public ArrayListVector(double[] array) {
        vector = new ArrayList(array.length);
        for (int i = 0; i < array.length; i++) {
            vector.add(array[i]);
        }
    };

    public ArrayListVector(final Vector vector) {
        if (vector == null)
            throw new IllegalArgumentException("Null vector is not allowed");
        Iterator vectorIterator = vector.iterator();
        this.vector = new ArrayList(vector.length());
        int index = 0;
        while (vectorIterator.hasNext()) {
            vector.set(index, (Double) vectorIterator.next());
            index++;
        }
    };

    @Override
    public int length() {
        return vector.size();
    }

    @Override
    public double get(int index) {
        return (Double) vector.get(index);
    }

    @Override
    public void set(int index, double value) {
        vector.set(index, value);
    }

    public Iterator<Double> iterator() {
        return vector.listIterator();
    }

    public double min() {
        Double min = Double.MAX_VALUE;
        for (Double value : vector) {
            if (min > value)
                min = value;
        }
        return min;
    }

    public double max() {
        Double max = Double.MIN_VALUE;
        for (Double value : vector) {
            if (max < value)
                max = value;
        }
        return max;
    }

    public Vector multiply(int multiplicator) {
        Vector newVector = new ArrayListVector(vector.size());
        int index = 0;
        for (Double value : vector) {
            newVector.set(index, value * multiplicator);
            index++;
        }
        return newVector;
    }

    public Vector multiply(Vector vector) {
        if (vector == null)
            return null;
        if (this.vector.size() != vector.length())
            throw new IllegalArgumentException();

        Vector newVector = new ArrayListVector(this.vector.size());

        int index = 0;
        for (Double value : vector) {
            newVector.set(index, value * vector.get(index));
            index++;
        }
        return newVector;
    }

    public Vector sum(Vector vector) {
        if (this.vector.size() != vector.length())
            throw new IllegalArgumentException();

        Vector newVector = new ArrayListVector(this.vector.size());

        int index = 0;
        for (Double value : vector) {
            newVector.set(index, value + vector.get(index));
            index++;
        }
        return newVector;
    }
}
